<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/14
 * Time: 9:16
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Suppliers extends Admin_Controller
{
    public function index()
    {
        $this->load->model('suppliers_model');

        $this->load->library('pagination');

        $assoc = $this->uri->uri_to_assoc();

        $_param_option = array(
            'supp_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text',
                'placeholder' => '填写供应商名称...'
            ),
            'status' => array(
                'col' => 'col-lg-1',
                'type' => 'option',
                'placeholder' => '选择状态',
                'option' => array(
                    1 => '没拉黑',
                    2 => '拉黑'
                )
            ),
        );

        $_param = array(
            'supp_name' => trim($this->input->get('supp_name')),
            'status' => trim($this->input->get('status')),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $config['base_url'] = base_url('suppliers/') . 'index/page/';
        $config['total_rows'] = $this->suppliers_model->supp_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->suppliers_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $_action_link = array(
            array('href' => base_url('suppliers/set'), 'name' => '添加供应商')
        );

        $data = array(
            'suppliers_list' => $this->suppliers_model->suppliers_list($_param),
            'param_option' => $_param_option,
            'action_link' => $_action_link,
            'param' => $_param,
            'pages' => $this->pagination->create_links()
        );

        $this->load->view('suppliers_list', $data);
    }

    public function set()
    {

        $this->load->model('suppliers_model');

        $assoc = $this->uri->uri_to_assoc();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->config->load('website_common');

        $config = array(
            array(
                'field' => 'suppliers_name',
                'label' => '供货商名称',
                'rules' => 'required',
                'errors' => array(
                    'required' => '请填写 %s.',
                ),
            )
        );

        $data = array(
            'supp_info' => $this->suppliers_model->suppliers_info(array('suppliers_id' => $assoc['supp_id'])),
            'supp_level' => $this->config->item('supp_level')
        );

        $this->form_validation->set_rules($config);

        if ($this->form_validation->run() == FALSE)
        {   
            $data['purchase'] = $this->db->from('purchase')->where(['is_delete'=>0])->get()->result_array();
            $this->load->view('suppliers_set', $data);
        }
        else
        {
            
            $data = array(
                'suppliers_name' => trim($this->input->post('suppliers_name')),
                'purchase_eid' => $this->input->post('purchase_eid'),
                'sp_level' => $this->input->post('sp_level'),
                'sn_fix' => $this->input->post('sn_fix'),
                'qq' => $this->input->post('qq'),
                'phone' => $this->input->post('phone'),
                'address' => $this->input->post('address'),
                'suppliers_desc' => $this->input->post('suppliers_desc')
            );

            $this->suppliers_model->set_supp($data, $assoc['supp_id'] ? array('suppliers_id' => $assoc['supp_id']) : array());

            redirect('Suppliers/index');
        }
    }

    //供应商拉黑
    public function pullblack(){
        $this->load->library('sys_message');
        $this->load->model('suppliers_model');
        $suppliers_id = $this->input->post('suppliers_id');
        $res = $this->suppliers_model->suppliers_status($suppliers_id);
        if($res == 1){
            $this->sys_message->success('成功!');
        }
    }

    public function url_img(){

        $page = $this->input->get('page');
        $query = $this->db->from('goods_album')
                ->limit(10,10 * $page)
                ->get();
        $goods_album = $query->result_array();
        foreach ($goods_album as $k => $v) {

            $https = substr($v['original_img'],0,5);
            if($https != 'https'){
                continue;
            }


            $img = substr($v['original_img'],0,-13);
            $img = substr($img,5);
            $img = 'http'.$img;
            $context = stream_context_create(
                array(
                    "http" => array(
                        "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
                    )
                )
            );


            if(@!file_get_contents($img, false, $context)){
                // $moren = '/album/2019/05/shopify/shopifymorentupian.jpg';
                // $this->db->where("album_id", $v['album_id']);
                // $this->db->update('goods_album', array('original_img' => $moren));
                continue;
            }
            
            $img =  file_get_contents($img, false, $context);
            
            $aa = '/album/2019/05/shopify/shopify'.time().rand(10000000,99999999).'.jpg';
            $res = '/usr/share/nginx/html/productc'.$aa;
            
            // $aa = '\album\2019\05\shopify\shopify'.time().rand(10000000,99999999).'.jpg';
            // $res = 'D:\phpstudy\PHPTutorial\WWW'.$aa;
            file_put_contents($res,$img);
            $this->db->where("album_id", $v['album_id']);
            $this->db->update('goods_album', array('original_img' => $aa));
        }

    }

    public function select_supp(){
        $this->load->model('suppliers_model');
        $res = $this->suppliers_model->suppliers_options();
        echo json_encode($res);
    }

    public function purchaseeid(){
        $supp_id = $this->input->post('supp_id');
        $purchase_eid = $this->db->from('suppliers')->where(['suppliers_id'=>$supp_id])->select('purchase_eid')->get()->row_array();
        echo $purchase_eid['purchase_eid'];
    }
}
